package org.linlinjava.litemall.wx.web;

import com.alibaba.fastjson.JSONObject;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.FollowRelation;
import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.WxFollowService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wx/follow")
@Validated
public class WxFollowController {
    @Autowired
    private WxFollowService followService;

    /**
     * 关注用户
     */
    @PostMapping("/follow/{followingId}/{userId}")
    public Object follow(@PathVariable Long userId,
                         @PathVariable Long followingId,
                         @RequestBody JSONObject jsonObject) {
        if (followService.follow(userId, followingId, jsonObject.getString("followingName"), jsonObject.getString("followingAvatar"))) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    /**
     * 取消关注
     */
    @PostMapping("/unfollow/{followingId}/{userId}")
    public Object unfollow(@PathVariable Long userId, @PathVariable Long followingId) {
        if (followService.unfollow(userId, followingId)) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    /**
     * 获取关注状态
     */
    @GetMapping("/status/{userId}/{followingId}")
    public Object followStatus(@PathVariable Long userId, @PathVariable Long followingId) {
        boolean isFollowing = followService.isFollowing(userId, followingId);
        return ResponseUtil.ok(isFollowing);
    }

    /**
     * 获取关注列表
     */
    @GetMapping("/followings/{userId}")
    public Object getFollowings(@PathVariable Long userId) {
        List<FollowRelation> followings = followService.getFollowings(userId);
        return ResponseUtil.ok(followings);
    }

    /**
     * 获取粉丝列表
     */
    @GetMapping("/followers")
    public Object getFollowers(@LoginUser Long userId) {
        List<FollowRelation> followers = followService.getFollowers(userId);
        return ResponseUtil.ok(followers);
    }
} 